package com.prs.crm.action.cdxh;

import java.util.Collection;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.annotation.Clew;
import com.prs.crm.domain.cdxh.distribution.DistributionReturn;
import com.prs.crm.domain.cdxh.distribution.DistributionReturnDetail;
import com.prs.crm.service.cdxh.DistributionReturnService;

/**
 * @author LanXiaoGang
 * @E-mail:lan9liang@163.com
 * @qq:7857553326
 * @version 创建时间：2011-1-10 上午11:10:59 简单说明
 * 
 *          ****配送打回Action
 */

public class DistributionReturnAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private DistributionReturnService distributionReturnService;

	private DistributionReturn distributionReturn;

	private Collection<DistributionReturnDetail> slist;

	private PaginationSupport pagination;

	private String disTotalMoney;
	
	private String[] pser;

	private String[] nums;
	private String[] idss;
	private String[] proCodes;
	private String[] proNames;
	private String[] proNorms;
	private String[] proColors;
	private String[] proPacks;

	@Action("listDistributionReturn")
	public String listDistributionReturn() {

		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				DistributionReturn.class);
		criteria.add(Restrictions.eq("redisSate", 1));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getDistributionReturnService().listByPage(
				criteria, pagination));

		return SUCCESS;
	}
	
	@Action("listDistributionReturnRecord")
	public String listDistributionReturnRecord() {

		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				DistributionReturn.class);
		criteria.add(Restrictions.eq("redisSate", 2));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getDistributionReturnService().listByPage(
				criteria, pagination));

		return SUCCESS;
	}
	
	@Action("viewDistributionReturn")
	public String viewDistributionReturn() {
		distributionReturn = this.getDistributionReturnService().get(
				distributionReturn.getId());
		slist = distributionReturn.getDetails();
		disTotalMoney = PriceProcess(distributionReturn.getReToltalprice());
		return SUCCESS;
	}
	
	@Action("editDistributionReturn")
	public String editDistributionReturn() {
		distributionReturn = this.getDistributionReturnService().get(
				distributionReturn.getId());
		slist = distributionReturn.getDetails();
		disTotalMoney = PriceProcess(distributionReturn.getReToltalprice());
		return SUCCESS;
	}

	@Clew(results = { "success", "none", "error" })
	@Action(value = "saveDisReturn", results = {
			@Result(name = "success", type = "redirect", location = "listDistributionReturn"),
			@Result(name = "error", type = "redirect", location = "listDistributionReturn") })
	public String saveDisReturn() {

		this.getDistributionReturnService().setReturnCandal(distributionReturn,
				nums, idss, proCodes,proNames,proNorms,proColors,proPacks,pser);
		this.setPrompt("配送单打回数据处理中....");
		return SUCCESS;
	}

	public DistributionReturnService getDistributionReturnService() {
		return distributionReturnService;
	}

	public void setDistributionReturnService(
			DistributionReturnService distributionReturnService) {
		this.distributionReturnService = distributionReturnService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public DistributionReturn getDistributionReturn() {
		return distributionReturn;
	}

	public void setDistributionReturn(DistributionReturn distributionReturn) {
		this.distributionReturn = distributionReturn;
	}

	public Collection<DistributionReturnDetail> getSlist() {
		return slist;
	}

	public void setSlist(Collection<DistributionReturnDetail> slist) {
		this.slist = slist;
	}

	public String getDisTotalMoney() {
		return disTotalMoney;
	}

	public void setDisTotalMoney(String disTotalMoney) {
		this.disTotalMoney = disTotalMoney;
	}

	public String NumZ(String Num) {
		String printValue = "零";
		int bvalue = Integer.parseInt(Num);
		if (bvalue == 0) {

		} else if (bvalue == 1) {
			printValue = "壹";
		} else if (bvalue == 2) {
			printValue = "贰";
		} else if (bvalue == 3) {
			printValue = "叁";
		} else if (bvalue == 4) {
			printValue = "肆";
		} else if (bvalue == 5) {
			printValue = "伍";
		} else if (bvalue == 6) {
			printValue = "陆";
		} else if (bvalue == 7) {
			printValue = "柒";
		} else if (bvalue == 8) {
			printValue = "捌";
		} else if (bvalue == 9) {
			printValue = "玖";
		}
		return printValue;
	}

	public String WPrint(String W) {
		String zOver = "";
		for (int i = 0; i < W.length(); i++) {
			zOver = zOver + NumZ(W.charAt(i) + "");
		}

		return zOver.trim();
	}

	public String PriceProcess(double prices) {
		String backValueA = "";
		String backValueB = "";
		String Figures[] = (prices + "").split("\\.");

		if (Figures[0].length() == 1) {
			backValueA = "零万零千零佰零拾零元";
		} else {
			if (Figures[0].length() == 1) {
				backValueA = "零万零千零佰零拾" + NumZ(Figures[0]) + "元";
			} else if (Figures[0].length() == 2) {
				backValueA = "零万零千零佰" + NumZ(Figures[0].charAt(0) + "") + "拾"
						+ NumZ(Figures[0].charAt(1) + "") + "元";
			} else if (Figures[0].length() == 3) {
				backValueA = "零万零千" + NumZ(Figures[0].charAt(0) + "") + "佰"
						+ NumZ(Figures[0].charAt(1) + "") + "拾"
						+ NumZ(Figures[0].charAt(2) + "") + "元";
			} else if (Figures[0].length() == 4) {
				backValueA = "零万" + NumZ(Figures[0].charAt(0) + "") + "千"
						+ NumZ(Figures[0].charAt(1) + "") + "佰"
						+ NumZ(Figures[0].charAt(2) + "") + "拾"
						+ NumZ((Figures[0].charAt(3) + "")) + "元";
			} else if (Figures[0].length() == 5) {
				backValueA = NumZ(Figures[0].charAt(0) + "") + "万"
						+ NumZ(Figures[0].charAt(1) + "") + "千"
						+ NumZ(Figures[0].charAt(2) + "") + "佰"
						+ NumZ(Figures[0].charAt(3) + "") + "拾"
						+ NumZ((Figures[0].charAt(4) + "")) + "元";
			} else if (Figures[0].length() > 5) {
				int firstA = Integer.parseInt(Figures[0]);
				int mang = firstA / 10000;
				int yu = firstA % 10000;
				String nn = yu + "";
				backValueA = WPrint(mang + "") + "万" + NumZ(nn.charAt(0) + "")
						+ "千" + NumZ(nn.charAt(1) + "") + "佰"
						+ NumZ(nn.charAt(2) + "") + "拾"
						+ NumZ((nn.charAt(3) + "")) + "元";

			}
		}
		if (Figures[1].length() == 1) {
			backValueB = "零角";
		} else {
			if (Figures[1].length() == 1) {
				backValueB = "零角";
			} else if (Figures[1].length() > 1) {
				int hx = Integer.parseInt(Figures[1].charAt(1) + "");
				if (hx >= 5) {
					int hxx = Integer.parseInt(Figures[1].charAt(0) + "") + 1;
					backValueB = NumZ(hxx + "") + "角";
				} else {
					backValueB = NumZ(Figures[1].charAt(0) + "") + "角";
				}

			}
		}
		String AB = backValueA + backValueB;

		return AB;

	}

	public String[] getNums() {
		return nums;
	}

	public void setNums(String[] nums) {
		this.nums = nums;
	}

	public String[] getIdss() {
		return idss;
	}

	public void setIdss(String[] idss) {
		this.idss = idss;
	}

	public String[] getProCodes() {
		return proCodes;
	}

	public void setProCodes(String[] proCodes) {
		this.proCodes = proCodes;
	}

	public String[] getProNames() {
		return proNames;
	}

	public void setProNames(String[] proNames) {
		this.proNames = proNames;
	}

	public String[] getProNorms() {
		return proNorms;
	}

	public void setProNorms(String[] proNorms) {
		this.proNorms = proNorms;
	}

	public String[] getProColors() {
		return proColors;
	}

	public void setProColors(String[] proColors) {
		this.proColors = proColors;
	}

	public String[] getProPacks() {
		return proPacks;
	}

	public void setProPacks(String[] proPacks) {
		this.proPacks = proPacks;
	}

	
	public String[] getPser() {
		return pser;
	}

	public void setPser(String[] pser) {
		this.pser = pser;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

}
